//
//  DynamicLocalizer.swift
//  Sample
//
//  Created by mac on 2025/7/11.
//  Copyright © 2025 Roy Marmelstein. All rights reserved.
//

// 文件：DynamicLocalizer.swift
import UIKit

/// 动态翻译工具（非侵入式，无单例）
final class DynamicLocalizer {
    
    class func translateToFrench(in view: UIView) {
        for subview in view.subviews {
            if let label = subview as? UILabel, let text = label.text {
                // Step 1: 通过英文 value 反查 key
                if let key = Bundle.main.findKey(for: text, in: "en") {
                    // Step 2: 通过 key 获取法文 value
                    let frenchText = Bundle.main.localizedString(for: key, language: "fr")
                    label.text = frenchText
                }
            } else if let button = subview as? UIButton, let title = button.currentTitle {
                if let key = Bundle.main.findKey(for: title, in: "en") {
                    let frenchText = Bundle.main.localizedString(for: key, language: "fr")
                    button.setTitle(frenchText, for: .normal)
                }
            }
            // 递归遍历子视图
            translateToFrench(in: subview)
        }
    }
}
